package com.reading.service.impl;

import com.reading.feign.UserFeignClient;
import com.reading.mapper.ShellRecordMapper;
import com.reading.mapper.TaskMapper;
import com.reading.pojo.*;
import com.reading.pojo.user.User;
import com.reading.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private ShellRecordMapper shellRecordMapper;

    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public List<Task> findAll(Integer userId) {
        return taskMapper.findAll(userId);
    }

    @Override
    public void updateOne(TaskVo taskVo) {
        taskMapper.updateOne(taskVo);
    }

    @Override
    public void receiveShell( DailyShell dailyShell) {
        //获取任务信息
        Task task = taskMapper.findOne(dailyShell.getId());
        //新建一个ShellRecord并赋值
        ShellRecord shell = new ShellRecord();
        shell.setNumber(task.getShellNumber());
        shell.setRecord(task.getContent());
        shell.setUserId(task.getUserId());
        shell.setCreateTime(new Date());
        //添加贝壳记录
        shellRecordMapper.save(shell);
        //修改该用户的贝壳数量
        User user = userFeignClient.findOne(dailyShell.getUserId());
        GlodVO glodVO = new GlodVO();
        glodVO.setGlod2(user.getUserglod2()+task.getShellNumber());
        glodVO.setId(user.getId());
        userFeignClient.updateUserGlod2(glodVO);

    }

    @Override
    public Task findOne(Integer id) {
        return taskMapper.findOne(id);
    }

    @Override
    public void updateAllStatus() {
        taskMapper.updateAllStatus();
    }

    @Override
    public void addTaskList(Integer id) {
        taskMapper.addTaskList(id);
    }

}
